package licode.unisop.client.controller;

import licode.unisop.client.api.SopPlatform;
import licode.unisop.client.api.SopPlatformMng;
import licode.unisop.client.utils.MethodCallUtil;
import licode.unisop.client.utils.SopResult;
import licode.unisop.client.utils.SopUserAuthQueryUtil;
import licode.unisop.client.vo.SopAuthBrief;
import licode.unisop.client.vo.SopAuthUser;
import licode.unisop.client.vo.SopCredential;
import licode.unisop.client.vo.SopUserBrief;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author licode
 */
@RestController
@RequestMapping("/uni-api/v1/auth/query")
public class SopAuthQryController extends SopBaseController {
    @Resource
    public SopPlatformMng platformMng;

    @Resource
    public SopUserAuthQueryUtil sopUserAuthQueryUtil;

    @GetMapping("/platform-token")
    public SopResult<String> platformToken(@RequestParam("name") String name) {
        SopPlatform plat = platformMng.getPlatform(name);
        return SopResult.of(null == plat ? "" : plat.getToken());
    }

    @PostMapping("/token-info")
    public SopResult<SopAuthBrief> tokenInfo() {
        return MethodCallUtil.exec(null, () -> sopUserAuthQueryUtil.getTokenInfo());
    }

    @PostMapping("/auth-user-info")
    public SopResult<SopAuthUser> authUserInfo() {
        return MethodCallUtil.exec(null, () -> sopUserAuthQueryUtil.authUserInfo(null));
    }

    @PostMapping("/user-info")
    public SopResult<SopUserBrief> userInfo() {
        return MethodCallUtil.exec(null, () -> sopUserAuthQueryUtil.getUserInfo(null));
    }

    @PostMapping("/user-assets")
    public SopResult<Map<String, Object>> resInfo() {
        return MethodCallUtil.exec(null, () -> sopUserAuthQueryUtil.getUserAssets(null));
    }

    @PostMapping("/user-cred")
    public SopResult<SopCredential> authInfo() {
        return MethodCallUtil.exec(null, () -> {
            checkPlatform();
            return sopUserAuthQueryUtil.getCredential(null);
        });
    }
}
